﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

using Telerik.Windows.Controls;
using Telerik.Windows.Controls.Scheduler;

namespace Centrica.TaskManager.Task.Calendar
{
    public abstract class SchedulerProviderBase : DependencyObject, ISchedulerProvider
    {

        protected RadScheduler scheduler;

        public abstract IEnumerable GetAppointments();

        public void Attach(RadScheduler schedulerToAttach)
        {
            this.scheduler = schedulerToAttach;
            this.UpdateAppointments();
            this.HookToSchedulerEvents();
        }

        /// <summary>
        /// Detaches the current <see cref="RadScheduler" /> from this instance.
        /// </summary>
        public void Detach()
        {
            if (this.scheduler != null)
            {
                this.UnHookToSchedulerEvents();
                this.scheduler.AppointmentsSource = null;
            }
        }

        /// <summary>
        /// Updates the appointments source of the scheduler.
        /// </summary>
        public void UpdateAppointments()
        {
            if (this.scheduler != null)
                this.scheduler.AppointmentsSource = this.GetAppointments();
        }

        protected virtual void OnAppointmentAdded(IAppointment appointment)
        {
        }

        protected virtual void OnAppointmentAdding(IAppointment appointment)
        {
        }

        protected virtual void OnAppointmentCreated(IAppointment appointment)
        {


        }

        protected virtual void OnAppointmentCreating(IAppointment appointment)
        {

        }

        protected virtual void OnAppointmentDeleted(IAppointment appointment)
        {

        }

        protected virtual void OnAppointmentDeleting(IAppointment appointment)
        {

        }

        protected virtual void OnAppointmentEdited(IAppointment appointment)
        {

        }

        protected virtual void OnAppointmentEditing(IAppointment appointment)
        {



        }

        private void OnSchedulerAppointmentAdded(object sender, AppointmentAddedEventArgs e)
        {
            this.OnAppointmentAdded(e.Appointment);
        }

        private void OnSchedulerAppointmentAdding(object sender, AppointmentAddingEventArgs e)
        {
            this.OnAppointmentAdding(e.Appointment);
        }

        private void OnSchedulerAppointmentCreated(object sender, AppointmentCreatedEventArgs e)
        {
            this.OnAppointmentCreated(e.CreatedAppointment);
        }

        private void OnSchedulerAppointmentCreating(object sender, AppointmentCreatingEventArgs e)
        {
           
            this.OnAppointmentCreating(e.NewAppointment);
        }

        private void OnSchedulerAppointmentDeleted(object sender, AppointmentDeletedEventArgs e)
        {
            this.OnAppointmentDeleted(e.Appointment);
        }

        private void OnSchedulerAppointmentDeleting(object sender, AppointmentDeletingEventArgs e)
        {
          
            this.OnAppointmentDeleting(e.Appointment);
        }

        private void OnSchedulerAppointmentEdited(object sender, AppointmentEditedEventArgs e)
        {
            this.OnAppointmentEdited(e.Appointment);
        }

        private void OnSchedulerAppointmentEditing(object sender, AppointmentEditingEventArgs e)
        {

            e.Cancel = true;

            this.OnAppointmentEditing(e.Appointment);
        }

        private void HookToSchedulerEvents()
        {

            this.scheduler.AppointmentAdded += this.OnSchedulerAppointmentAdded;
            this.scheduler.AppointmentAdding += this.OnSchedulerAppointmentAdding;
            this.scheduler.AppointmentCreated += this.OnSchedulerAppointmentCreated;
            this.scheduler.AppointmentCreating += this.OnSchedulerAppointmentCreating;
            this.scheduler.AppointmentDeleted += this.OnSchedulerAppointmentDeleted;
            this.scheduler.AppointmentDeleting += this.OnSchedulerAppointmentDeleting;
            this.scheduler.AppointmentEdited += this.OnSchedulerAppointmentEdited;
            this.scheduler.AppointmentEditing += this.OnSchedulerAppointmentEditing;

        }

        private void UnHookToSchedulerEvents()
        {

            this.scheduler.AppointmentAdded -= this.OnSchedulerAppointmentAdded;
            this.scheduler.AppointmentAdding -= this.OnSchedulerAppointmentAdding;
            this.scheduler.AppointmentCreated -= this.OnSchedulerAppointmentCreated;
            this.scheduler.AppointmentCreating -= this.OnSchedulerAppointmentCreating;
            this.scheduler.AppointmentDeleted -= this.OnSchedulerAppointmentDeleted;
            this.scheduler.AppointmentDeleting -= this.OnSchedulerAppointmentDeleting;
            this.scheduler.AppointmentEdited -= this.OnSchedulerAppointmentEdited;
            this.scheduler.AppointmentEditing -= this.OnSchedulerAppointmentEditing;

        }






    }
}

